package com.example.hrandroidapp.adapter;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.hrandroidapp.R;
import com.example.hrandroidapp.common.Constant;
import com.example.hrandroidapp.domain.ServerPO;
import com.example.hrandroidapp.preferences.CurrentServerPreference;
import com.example.hrandroidapp.preferences.ServerListPreference;
import com.gc.materialdesign.views.CheckBox;
import com.gc.materialdesign.views.CheckBox.OnCheckListener;

public class ServerListViewAdapter extends BaseAdapter {

	private Context context;
	private List<ServerPO> lstServers = new ArrayList<ServerPO>();
	private EditServerInterface callback;
	
	public ServerListViewAdapter(Context context, List<ServerPO> lstServers, EditServerInterface callback) {
		this.context=context;
		this.lstServers=lstServers;
		this.callback=callback;
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return lstServers.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return lstServers.get(position);
	}
	
	public List<ServerPO> getServerList() {
		return lstServers;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		if(convertView == null) {
			LayoutInflater mInflater = (LayoutInflater)context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = mInflater.inflate(R.layout.server_item, null);
		}
		
		TextView tvSvrName = (TextView) convertView.findViewById(R.id.tv_server_name);
		TextView tvSvrIP = (TextView) convertView.findViewById(R.id.tv_server_ip);
		
		ImageView imgEdit = (ImageView) convertView.findViewById(R.id.img_edit);
		imgEdit.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				callback.editServerCallback(lstServers.get(position), position);
			}
		});
		
		CheckBox chkServer = (CheckBox) convertView.findViewById(R.id.chk_server);
		chkServer.setChecked(lstServers.get(position).isCheck()); 
		lstServers.get(position).setCheck(chkServer.isCheck());

		if(chkServer.isCheck()){
			tvSvrIP.setTextColor(context.getResources().getColor(R.color.success_color));
			Constant.setCurrentServer(lstServers.get(position).getSvrName(), lstServers.get(position).getSvrIP());
			CurrentServerPreference.updateCurrentServer(context, lstServers.get(position));
		}else { 
			tvSvrIP.setTextColor(context.getResources().getColor(R.color.mail_msg_gray));
		}
		
		chkServer.setOncheckListener(new OnCheckListener() {
			
			@Override
			public void onCheck(boolean check) {
				setServers(lstServers, position);
			}
		});
		
		tvSvrName.setText(lstServers.get(position).getSvrName());
		tvSvrIP.setText(lstServers.get(position).getSvrIP());
		
		return convertView;
	}
	
	public void setServers(List<ServerPO> lstServer, int pos) {
		for(int i=0; i<lstServer.size(); i++) {
			if(i == pos) {
				lstServer.get(i).setCheck(true);
				CurrentServerPreference.updateCurrentServer(context, lstServer.get(i));
			}else {
				lstServer.get(i).setCheck(false);
			}
		}
		this.lstServers=lstServer;
		notifyDataSetChanged();
	}
	
	public static interface EditServerInterface {
		void editServerCallback(ServerPO svrPO, int position);
	}

}
